﻿Imports System.ComponentModel

Namespace Controle

    <Designer("YCDotNetFramework.YCControles.YCModalPopUp")> _
    Public Class YCModalPopUp
        Inherits WebControl
        Implements INamingContainer

        Private pTitulo As String = Nothing
        Private pImgFechar As String = Nothing

        Private pBodyControl As Control
        Private pBody As ITemplate = Nothing

        Private pHfIsOpen As HtmlControls.HtmlInputHidden

        Private pWidth As Unit = 0

#Region "Propriedades"

        <PersistenceMode(PersistenceMode.InnerProperty), Browsable(False), TemplateInstance(TemplateInstance.Single)> _
        Public Property Body() As ITemplate
            Get
                Return pBody
            End Get
            Set(ByVal value As ITemplate)
                pBody = value
            End Set
        End Property

        Public Property Titulo() As String
            Get
                Return pTitulo
            End Get
            Set(ByVal value As String)
                pTitulo = value
            End Set
        End Property

        'TODO: 
        <UrlProperty()> _
        Public Property ImgFechar() As String
            Get
                Return pImgFechar
            End Get
            Set(ByVal value As String)
                pImgFechar = value
            End Set
        End Property

        Public Property IsOpen() As Boolean
            Get
                If pHfIsOpen.Value Is String.Empty Then Return False
                Return pHfIsOpen.Value
            End Get
            Set(ByVal value As Boolean)
                If Not String.IsNullOrEmpty(value) Then _
                    pHfIsOpen.Value = value
            End Set
        End Property

        ''' <summary>
        ''' Se não passar o Width, será atribuido o valor padrão de 780 px
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overloads Property Width() As Unit
            Get
                If pWidth = 0 Then pWidth = 780
                Return pWidth
            End Get
            Set(ByVal value As Unit)
                pWidth = value
            End Set
        End Property


#End Region

        Protected Sub CreateContents()
            Me.Controls.Clear()
            MyBase.CreateChildControls()

            Dim hdnField As New HtmlControls.HtmlInputHidden
            hdnField.ID = "hfIsOpen"
            AddHandler hdnField.ServerChange, AddressOf HiddenFieldValueChanged
            MyBase.Controls.Add(hdnField)

            pHfIsOpen = hdnField

            pBodyControl = New Control
            Me.Controls.Add(pBodyControl)

            '---------------------------
            If pBody IsNot Nothing Then
                pBody.InstantiateIn(pBodyControl)
            End If
        End Sub

        Private Sub HiddenFieldValueChanged(ByVal sender As Object, ByVal e As EventArgs)
            pHfIsOpen.Value = DirectCast(sender, HtmlControls.HtmlInputHidden).Value
        End Sub

        Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
            MyBase.OnInit(e)
            CreateContents()
            MyBase.EnsureChildControls()
        End Sub

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)
            If IsOpen Then
                Web.Javascript.ScriptBlock(String.Concat("function JQFunc(iddiv) { $('.bgmodal').height($(window).height());if ($(window).height() < $('#div_' + iddiv).height()) {$('#div_' + iddiv).css('height', $(window).height() - 20 + 'px');} $('#div_' + iddiv).css('top', ($(window).height()/2) - (($('#div_' + iddiv + '>.ctnmodal').height() + 24)/2) + 'px');$('#div_' + iddiv).css('left', ($(window).width()/2) - ($('#div_' + iddiv).width() / 2) + 'px');}"))
                Web.Javascript.Executa(String.Concat("function JQAction", Me.ClientID, "() {$(document).ready(function() {JQFunc('", Me.ClientID, "');});$(window).resize(function() {JQFunc('", Me.ClientID, "');});}JQAction", Me.ClientID, "();function pageLoad() { JQAction", Me.ClientID, "(); }"))
            End If
        End Sub

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            MyBase.RenderBeginTag(writer)

            With writer
                pHfIsOpen.RenderControl(writer)

                If Not String.IsNullOrEmpty(pHfIsOpen.Value) AndAlso pHfIsOpen.Value Then

                    .Write("<div class=""YCBgModal"" style=""background: #000; width: 100%; position: fixed; opacity: 0.4; top: 0px; left: 0px; height: 100%;""></div>")
                    .Write(String.Format("<div id=""div_{0}"" class=""YCModal"" style=""width: {1}px; left: {2}px; position: fixed;"" >", Me.ClientID, Width.Value, Width.Value / 2))
                    .Write("  <div class=""titulomodal""><span class=""titulomodaltext"">")
                    .Write(pTitulo)
                    .Write("</span>    <span class=""fechar"">")
                    .Write(String.Format("<a href=""javascript:void(0);"" onclick=""document.getElementById('{0}').value=false;document.getElementById('{1}').style.display='none';""><img src=""{2}"" alt=""x"" title=""Fechar"" /></a>", pHfIsOpen.ClientID, Me.ClientID, IIf(pImgFechar Is Nothing, "x", ResolveUrl(pImgFechar))))
                    .Write("    </span>")
                    .Write("  </div>")
                    .Write("  <div class=""ctnmodal"">")
                    pBodyControl.RenderControl(writer)
                    .Write("  </div>")
                    .Write("</div>")

                End If

            End With

            MyBase.RenderEndTag(writer)
        End Sub

    End Class

End Namespace
